package com.threeti.mecool.core.domain.model.plan.aspects;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;

import org.springframework.beans.factory.annotation.Configurable;

import com.threeti.mecool.core.domain.model.plan.pojo.Job;
import com.threeti.mecool.core.domain.model.plan.pojo.UserJob;
import com.threeti.mecool.core.domain.model.plan.pojo.UserStep;

privileged aspect UserJob_AJ_Entity {
	declare @type: UserJob:@Configurable;
	declare @type: UserJob:@Entity;
  declare @type: UserJob:@Table(name = "DM_WF_USER_JOB");
	
	declare @field: Long UserJob.id:@Id;
	declare @field: Long UserJob.id:@GeneratedValue(strategy = GenerationType.AUTO);
	declare @field: Integer UserJob.version:@Version;
	
	declare @field: Long UserJob.userId:@Column(name = "USER_ID");
	declare @field: Job UserJob.job:@ManyToOne(optional = false);
	declare @field: List<UserStep> UserJob.userSteps:@OneToMany(mappedBy = "userJob", cascade = CascadeType.ALL, orphanRemoval = true);

	before(UserJob userJob): call(Job UserJob.getJob()) && target(userJob) && !within(UserJob_AJ_Entity) {
		if (userJob.getJob() == null && userJob.getJobId() != null) {
			Job job = Job.findById(userJob.getJobId());
			userJob.setJob(job);
		}
	}

}
